package com.cxb.view;

import com.cxb.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PM25View extends View{
	private Context context;
	private Paint mpaint;
	private int pm25value;
	private final int MAXVALUE = 500;
	private int color;
	private int colordeep = 0x333333;
	private int colorshallow = 0xc3c3c3;
	private int index = 0;
	
	private int radius = 26;
	private int height = 15;
	
	private Handler handler;

	public PM25View(Context context) {
		super(context);
		init(context);
	}
	public PM25View(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	public PM25View(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context)
	{
		colordeep = context.getResources().getColor(R.color.pm25_bg_deep);
		colorshallow = context.getResources().getColor(R.color.pm25_bg_shallow);
		this.setBackgroundResource(R.drawable.compass_bg);
		mpaint = new Paint();
		mpaint.setColor(context.getResources().getColor(R.color.text_normal_color));
		mpaint.setStyle(Style.FILL_AND_STROKE);
		mpaint.setStrokeWidth(5);
		mpaint.setAntiAlias(true);
		radius = (int)context.getResources().getDimension(R.dimen.main_pm_r);
		height = (int)context.getResources().getDimension(R.dimen.main_pm_height);
//		testModel();
	}
	
	public void setPM25Value(int value)
	{
		if(value < 0)
			value = 0;
		if(value > 500)
			value = 500;
		pm25value = value;
		this.invalidate();
	}
	
	public void testModel()
	{
		
		final Handler handler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if(msg.what == 20003)
				{
					setPM25Value(index++);
					if(index>500)
						index = 0;
				}
			}
			
		};
		
		new Thread(){
			public void run() {
				while(true)
				{
				handler.sendEmptyMessage(20003);
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
			}
		}.start();
				
		
	}
	
	private double getDegree(int pm25value)
	{
		float div = ((float)pm25value / (float)MAXVALUE);
		double degree = (div*Math.PI);
		return degree;
	}
	
	private float getDiv(int pm25value)
	{
		return ((float)pm25value / (float)MAXVALUE);
	}
	
	private int getRadius()
	{
		return radius;
	}
	
	private int getLineHeight()
	{
		return height;
	}

	private void drawline(Canvas canvas, int height, int width)
	{
	
		double degreeValue = getDegree(this.pm25value);
		int centerX = width / 2;
		int centerY = height / 2;
		int linex1 = centerX - (int)(getRadius() * Math.cos(degreeValue));
		int liney1 = centerY + (int)(getRadius() * Math.sin(degreeValue));
		int linex2 = linex1 - (int)(getLineHeight() * Math.cos(degreeValue));
		int liney2 = liney1 + (int)(getLineHeight() * Math.sin(degreeValue));
		if(degreeValue > Math.PI / 2)
			mpaint.setColor(colorshallow);
		else
			mpaint.setColor(colordeep);
		canvas.drawLine(linex1, liney1, linex2, liney2, mpaint);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawline(canvas,this.getMeasuredHeight(), this.getMeasuredWidth());
		
	}

	
	
}
